package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.studentManager.model.ReadDocument;
import com.hdoit.kylin.logical.studentManager.service.IReadDocumentService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.viewmodel.postgraduate.ReadDocumentSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author anshengqiang
 * @email
 * @createtime 2017年07月26日 16:23:02
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/postgraduate/readDocument/")
public class ReadDocumentController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/postgraduate/readDocument/";
	}

	private IReadDocumentService readDocumentService;

	public IReadDocumentService getReadDocumentService() {
		return readDocumentService;
	}
	
	private IStudentService studentService;
	public IStudentService getIStudentService(){
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	@Resource
	public void setReadDocumentService(IReadDocumentService readDocumentService) {
		this.readDocumentService = readDocumentService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "readDocumentDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel, ReadDocumentSearch search, boolean ajaxPost) {
		pagerModel.setTotalAmount(readDocumentService.queryCount(search));
		List<ReadDocument> readDocuments = readDocumentService.queryList(search, pagerModel);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("readDocuments", readDocuments);
		model.addAttribute("readDocumentSearch", search);
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET, RequestMethod.POST})
	public String readDocumentDetailed(Model model, String key) {
		ReadDocument readDocument = new ReadDocument();
		if (!StringUtils.isEmpty(key)) {
			readDocument = readDocumentService.queryModel(key);
		}
		model.addAttribute("readDocument", readDocument);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = {  RequestMethod.GET, RequestMethod.POST })
	public String save(Model model, ReadDocument readDocument) {
		if (StringUtils.isEmpty(readDocument.getId())) {
			readDocument.setId(CommonUtils.getKeyId());
			readDocument.setModelStatus(ModelStatus.add);
			readDocument.setEmployeeId(getLoginModel().getLoginId());
		} else {
			readDocument.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if (readDocumentService.applyChange(readDocument)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		model.addAttribute("readDocument", readDocument);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (readDocumentService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}